package org.lanqiao.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.lanqiao.entity.WxuserGift;
import org.lanqiao.service.GiftService;
import org.lanqiao.service.WxuserGiftService;
import org.lanqiao.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/gift")
@Validated
@ResponseBody
public class GiftController {

    @Autowired
    GiftService giftService;
    @Autowired
    WxuserGiftService wxuserGiftService;

    /**
     * 查询所有礼物---根剧微信id
     *
     * @param currentPage
     * @return
     */
    @GetMapping("/getAllGift/{wxId}/{currentPage}")
    public Result getAllGift(@PathVariable("currentPage")@NotNull(message = "页码不可为空") int currentPage,
                             @PathVariable("wxId")@NotNull(message = "微信id不可为空") String wxId){
        PageHelper.startPage(currentPage,10);//每页记录数10条
        //得到分页的结果对象
        PageInfo<Map> pageInfo = new PageInfo<Map>(giftService.getFGift(wxId));
        return Result.ok(pageInfo);
    }

    /**
     * 微信id和活动id一期查
     * @param
     * @return
     */
    @GetMapping("/getGiftByAll/{wxId}/{activityId}")
    public Result getGiftByAll(@PathVariable("wxId")@NotNull(message = "微信id不可为空") String wxId,
                               @PathVariable("activityId")@NotNull(message = "活动id不可为空") String activityId) {
        return Result.ok(giftService.getGiftByAll(wxId,activityId));
    }

    /**
     * 根据活动id查询所有礼物
     *
     * @return
     */
    @GetMapping(value = "/getGiftByActivityId")
    public Result getGiftByActivityId(String activityId,@RequestParam(value = "id", defaultValue = "1") int currentPage) {
        //设置每页显示的数据条数
        PageHelper.startPage(currentPage, 5);
        //获取查询到的数据
        Result result = giftService.getGiftByActivityId(activityId);
        List data = (List) result.getData();
        //得到分页的结果对象
        PageInfo<WxuserGift> pageInfo = new PageInfo<WxuserGift>(data);
        result.setData(pageInfo);
        return result;
    }

    @RequestMapping("/addGift")
    public  Result addGift(String wxid, String acid, String income){

        return wxuserGiftService.addGift(wxid,acid,income);
    }

    /*
     * 查询所有礼物信息
     * */
    @GetMapping(value = "/selectAllGift")
    public Result selectAllGift() {
        Result result = giftService.selectAllGift();
        return result;

    }
}
